package com.lonely.purchase.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lonely.common.core.domain.TimeLine;
import com.lonely.purchase.domain.entity.SysRemainingMaterialInventoryLog;
import com.lonely.purchase.mapper.SysRemainingMaterialInventoryLogMapper;
import com.lonely.purchase.service.IRemainingMaterialInventoryLogService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author : 黄志标
 * @date: 2025/7/28 11:33
 * @description: 余料库存日志服务实现类
 */
@Service
public class RemainingMaterialInventoryLogServiceImpl extends ServiceImpl<SysRemainingMaterialInventoryLogMapper, SysRemainingMaterialInventoryLog> implements IRemainingMaterialInventoryLogService {


    /**
     * 获取余料库存日志时间线
     *
     * @param inventoryId
     * @return
     */
    @Override
    public List<TimeLine> getLogTimeLine(Long inventoryId) {

        List<SysRemainingMaterialInventoryLog> inventoryLogs = super.list(new LambdaQueryWrapper<SysRemainingMaterialInventoryLog>()
                .eq(SysRemainingMaterialInventoryLog::getRemainingMaterialInventoryId, inventoryId)
                .orderByDesc(SysRemainingMaterialInventoryLog::getCreateTime)
        );
        if (CollUtil.isEmpty(inventoryLogs)) {
            return new ArrayList<>();
        }

        return inventoryLogs.stream().map(x -> {
            TimeLine timeLine = new TimeLine();
            timeLine.setType("info");
            timeLine.setContent(null);
            timeLine.setTitle(x.getLogTitle());
            timeLine.setTime(DateUtil.format(x.getCreateTime(), "yyyy-MM-dd HH:mm"));
            return timeLine;
        }).collect(Collectors.toList());
    }
}
